<template>
    <div>
        <el-breadcrumb separator-icon="ArrowRight" style="margin: 16px">
            <!-- <el-breadcrumb-item :to="{ path: '/ManageHome' }">首页</el-breadcrumb-item> -->
            <el-breadcrumb-item>大模型</el-breadcrumb-item>
            <el-breadcrumb-item>模型管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card style="margin: 15px; min-height: calc(100vh - 111px)">
            <div>
                <!--    功能区-->
                <div style="margin: 10px 0">
                    <!--    搜索区-->
                    <div style="margin: 10px 0">
                        <span
                            style="margin-left: 40px; margin-right: 14px; font-size: 15px; font-weight: 700;">模型厂商</span>
                        <el-input v-model="provider" size="small" clearable placeholder="模型厂商"
                            prefix-icon="el-icon-search" style="width: 15%;" />
                        <span
                            style="margin-left: 40px; margin-right: 14px; font-size: 15px; font-weight: 700;">模型名称</span>
                        <el-input v-model="name" size="small" clearable placeholder="模型名称" prefix-icon="el-icon-search"
                            style="width: 15%;" />
                        <el-button icon="el-icon-search" @click="load"
                            style="background-color: #143371; color: aliceblue;" size="small">搜索</el-button>
                        <el-button icon="el-icon-refresh-left" style="margin-left: 5px;" @click="reset"
                            size="small">重置</el-button>
                        <div style="float: right">
                            <el-tooltip content="添加模型" placement="top">
                                <el-button style="background-color: #143371; color: aliceblue;" size="small"
                                    @click="add">+新增</el-button>
                            </el-tooltip>
                        </div>
                    </div>
                </div>
                <!--    表格-->
                <el-table v-loading="loading" :data="tableData" max-height="1000" style="width: 100%">
                    <el-table-column v-if="false" label="序号" type="index" />
                    <el-table-column v-if="false" label="模型ID" prop="id" />
                    <el-table-column label="模型产商" prop="provider" />
                    <el-table-column label="模型名称(版本)" prop="model" />
                    <el-table-column label="生成随机性" prop="temperature"></el-table-column>
                    <el-table-column label="Top p" prop="topP" />
                    <el-table-column label="apiKey" prop="apiKey" />
                    <el-table-column label="secretKey" prop="secretKey" />
                    <el-table-column v-if="false" label="baseUrl" width="76px" prop="baseUrl" />
                    <el-table-column v-if="false" label="endpoint" width="152px" prop="endpoint">

                    </el-table-column>

                    <!--      操作栏-->
                    <el-table-column label="操作" width="150px">
                        <template slot-scope="scope">
                            <i @click="handleEdit(scope.$index, scope.row)" class="el-icon-edit"
                                style="margin-right: 5px;"></i>
                            <i @click="handleDelete(scope.$index, scope.row)" class="el-icon-delete"
                                style="margin-right: 5px;"></i>
                            <i @click="detail(scope.$index, scope.row)" class="el-icon-view"></i>
                        </template>
                    </el-table-column>
                </el-table>
                <!--分页-->
                <div style="margin: 10px 0">
                    <el-pagination :currentPage="currentPage" :page-size="pageSize" :page-sizes="[10, 20]"
                        :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange"
                        @current-change="handleCurrentChange">
                    </el-pagination>
                </div>

                <div v-loading="dialogloading">
                    <!--  弹窗-->
                    <el-dialog :visible.sync="dialogVisible" title="操作" width="50%" @close="cancel" top="30px">
                        <div v-if="isDetail == 0">
                            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                                <el-form-item label="模型厂商" prop="provider">
                                    <el-input v-model="form.provider" style="width: 80%" placeholder="模型厂商"></el-input>
                                </el-form-item>
                                <el-form-item label="模型名称(版本)" prop="model">
                                    <el-input v-model="form.model" style="width: 80%" placeholder="模型名称"></el-input>
                                </el-form-item>
                                <el-form-item label="生成随机性" prop="temperature">
                                    <el-slider style="width: 80%;" v-model="form.temperature" step="0.01"
                                        max="1"></el-slider>
                                </el-form-item>
                                <el-form-item label="Top p" prop="topP">
                                    <el-slider style="width: 80%;" v-model="form.topP" step="0.01" max="1"></el-slider>
                                </el-form-item>
                                <el-form-item label="apiKey" prop="apiKey">
                                    <el-input v-model="form.apiKey" style="width: 80%" placeholder="apiKey"></el-input>
                                </el-form-item>
                                <el-form-item label="secretKey" prop="secretKey">
                                    <el-input v-model="form.secretKey" style="width: 80%"
                                        placeholder="secretKey"></el-input>
                                </el-form-item>
                                <el-form-item label="baseUrl" prop="baseUrl">
                                    <el-input v-model="form.baseUrl" style="width: 80%"
                                        placeholder="baseUrl"></el-input>
                                </el-form-item>
                            </el-form>

                        </div>
                        <div v-else>

                            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                                <el-form-item label="模型厂商" prop="provider">
                                    <el-input v-model="form.provider" style="width: 80%" placeholder="模型厂商"></el-input>
                                </el-form-item>
                                <el-form-item label="模型名称(版本)" prop="model">
                                    <el-input v-model="form.model" style="width: 80%" placeholder="模型名称"></el-input>
                                </el-form-item>

                                <el-form-item label="生成随机性" prop="temperature">
                                    <el-slider style="width: 80%;" v-model="form.temperature" step="0.01"
                                        max="1"></el-slider>
                                </el-form-item>
                                <el-form-item label="Top p" prop="topP">
                                    <el-slider style="width: 80%;" v-model="form.topP" step="0.01" max="1"></el-slider>
                                </el-form-item>
                                <el-form-item label="apiKey" prop="apiKey">
                                    <el-input v-model="form.apiKey" style="width: 80%" placeholder="apiKey"></el-input>
                                </el-form-item>
                                <el-form-item label="secretKey" prop="secretKey">
                                    <el-input v-model="form.secretKey" style="width: 80%"
                                        placeholder="secretKey"></el-input>
                                </el-form-item>
                                <el-form-item label="baseUrl" prop="baseUrl">
                                    <el-input v-model="form.baseUrl" style="width: 80%"
                                        placeholder="baseUrl"></el-input>
                                </el-form-item>


                            </el-form>


                        </div>
                        <template #footer>
                            <span class="dialog-footer" v-if="isDetail == 0">
                                <el-button @click="cancel">取 消</el-button>
                                <el-button type="primary" @click="save">确 定</el-button>
                            </span>
                        </template>
                    </el-dialog>
                </div>
            </div>
        </el-card>
    </div>
</template>
<script>
import request from '@/utils/request'
import { Upload } from 'element-ui';
import { provide } from 'vue';
export default {
    name: "ModelManage",
    data() {
        return {
            color: '#000000',
            dialogloading: false,
            isDetail: 0,
            value: null,
            loading: true,
            editJudge: true,
            judge: false,
            dialogVisible: false,
            provider: "",
            name: "",
            currentPage: 1,
            pageSize: 10,
            total: 0,
            mode: 'default',
            tableData: [
                {
                    "id": "82fd7bc5f3bfce4b2732f6574decf23b",
                    "model": "gpt-3.5-turbo",
                    "provider": "OPENAI",
                    "temperature": 0.8,
                    "topP": 0.8,
                    "apiKey": "123",
                    "secretKey": null,
                    "baseUrl": null,
                    "endpoint": null,
                },
                {
                    "id": "82fd7bc5f3bfce4b2732f6574decf23b",
                    "model": "gpt-3.5-turbo",
                    "provider": "OPENAI",
                    "temperature": 0.8,
                    "topP": 0.8,
                    "apiKey": "123",
                    "secretKey": null,
                    "baseUrl": null,
                    "endpoint": null,
                },
            ],
            form: {
                "id": "82fd7bc5f3bfce4b2732f6574decf23b",
                "type": "CHAT",
                "model": "gpt-3.5-turbo",
                "provider": "OPENAI",
                "responseLimit": 3626,
                "temperature": 0.8,
                "topP": 0.8,
                "apiKey": "123",
                "secretKey": null,
                "baseUrl": null,
                "endpoint": null,
            },
            rules: {
                model: [{ required: true, message: "请输入", trigger: "blur" }],
                provider: [{ required: true, message: "请输入", trigger: "blur" }],
                apiKey: [{ required: true, message: "请输入", trigger: "blur" }],
                topP: [{ required: true, message: "请输入", trigger: "blur" }],
                temperature: [{ required: true, message: "请输入", trigger: "blur" }]
            },
            editDisplay: {
                display: "block",
            },
            display: {
                display: "none",
            },
        };
    },
    created() {
        this.load();
        this.loading = false;
        setTimeout(() => {
            //设置延迟执行
            this.loading = false;
        }, 1000);
    },
    methods: {
        async load() {
            request.get("/model/list", {
                params: {
                    pageNum: this.currentPage,
                    pageSize: this.pageSize,
                    provider: this.provider,
                    name: this.name
                }
            }).then(res => {
                if (res.code === "1") {
                    console.log("res", res)
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                    this.loading = false;
                }

            })
        },
        reset() {
            this.provider = null
            this.value = null,
                this.currentPage = 1;
            this.name = null
            this.pageSize = 10
            this.load()
        },
        detail(index, row) {
            this.isDetail = 1;
            this.dialogVisible = true;
            // 赋值数据
            this.$nextTick(() => {
                this.form = row
            });
        },
        handleDelete(index, row) {
            // 确认框确认是否要删除
            this.form = row;
            this.$confirm("确定要删除的信息吗?", "删除提示", {
                iconClass: "el-icon-question", //自定义图标样式
                confirmButtonText: "残忍删除", //确认按钮文字
                cancelButtonText: "取消删除", //取消按钮文字
                showClose: true, //是否显示右上角关闭按钮
                type: "warning", //提示类型  success/info/warning/error
            }).then(() => {  //选择确认按钮进入此方法
                request.post("/model/delete", this.form).then(res => {
                    if (res.code === "1") {
                        this.$message({
                            type: 'success',
                            message: '删除成功'
                        })
                    } else {
                        this.$message({
                            type: 'success',
                            message: '删除失败'
                        })
                    }
                    this.form = null;
                    this.name = "",
                    this.provider = "",
                    this.load();
                })
            }).catch(() => { //选择取消按钮进入此方法
                //取消操作

            });
        },
        handleEdit(index, row) {
            this.judge = true;
            this.isDetail = 0;
            this.dialogVisible = true;
            this.$nextTick(() => {
                this.form = row
            });
        },
        cancel() {
            this.form = {}
            this.dialogVisible = false
        },
        save() {
            this.$refs.form.validate(vaild => {
                if (vaild) {
                    console.log(1111)
                    if (this.judge) {
                        request.post("/model/update", this.form).then(res => {
                            if (res.code === "1") {
                                this.$message({
                                    type: 'success',
                                    message: '修改成功'
                                })
                                this.name = "",
                                this.provider = "",
                                this.load();
                                this.dialogVisible = false
                            }
                            else {
                                this.$message({
                                    type: 'error',
                                    message: res.msg
                                })
                            }
                        })
                    }
                    else {
                        request.post("/model/add", this.form).then(res => {
                            if (res.code === "1") {
                                this.$message({
                                    type: 'success',
                                    message: '添加成功'
                                })
                                this.name = "",
                                this.provider = "",
                                this.load();
                                this.dialogVisible = false
                            }
                            else {
                                this.$message({
                                    type: 'error',
                                    message: res.msg
                                })
                            }
                        })
                        const loading = this.$loading({
                            lock: true,
                            text: '添加中',
                            spinner: 'el-icon-loading',
                            background: 'rgba(0, 0, 0, 0.7)'
                        });
                    }
                }
            })
        },
        formatDate(chinaTime) {
            const year = chinaTime.getFullYear();
            const month = (chinaTime.getMonth() + 1).toString().padStart(2, '0');
            const day = chinaTime.getDate().toString().padStart(2, '0');
            const hours = chinaTime.getHours().toString().padStart(2, '0');
            const minutes = chinaTime.getMinutes().toString().padStart(2, '0');
            const seconds = chinaTime.getSeconds().toString().padStart(2, '0');
            return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        },
        add() {
            this.dialogVisible = true;
            this.isDetail = 0;
            this.judge = false;
            this.form = {}
        },
        handleSizeChange(pageSize) {
            //改变每页个数
            this.pageSize = pageSize;
            this.load();
        },
        handleCurrentChange(pageNum) {
            //改变页码
            this.currentPage = pageNum;
            this.load();
        },
        formatTooltip(value) {
            return value / 100;
        }
    }


}
</script>
<style>
.demo-table-expand {
    font-size: 0;
}

.demo-table-expand label {
    width: 150px;
    color: #99a9bf;
}

.demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 90%;
    display: block;
}

.demo-table-expand .el-form-item p {
    height: 30px;
    padding: 0px 0px;
    margin-bottom: 10px;
    margin-top: 10px;
}

.el-upload-list__item {
    transition: none !important;
}
</style>